package scheduler.core.task;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import scheduler.core.utils.TaskPoolValidationException;
import scheduler.core.utils.TaskjValidationException;
        
/**
 *
 * @author Volodymyr Pogorelov
 */
public class TaskPool  extends TaskPoolBase{
    private Map<Integer, Taskj> tasks; // множина задач і-го пріорітету

    public TaskPool(int prior, int capacity) throws TaskPoolValidationException {
        super(prior);
        tasks = new HashMap<>(capacity);
    }
    
    @Override
    public double getLambdai(){
        if(tasks.isEmpty()){
            return 0;
        }
        double res = 0;
        for(Taskj task : tasks.values()){
            res += task.getAj();
        }
        res /= tasks.size();
        return res;
    }
    
    @Override
    public double getNui(){
        if(tasks.isEmpty()){
            return 0;
        }
        double res = 0;
        for(Taskj task : tasks.values()){
            res += task.getBj();
        }
        res /= tasks.size();
        return res;
    }
    
    public void removeTask(int id) throws TaskjValidationException{
       tasks.remove(id);
    }
    
    public void addTask(Taskj taskj){
        tasks.put(taskj.getId(), taskj);
    }
    
    @Override
    public int getN(){
        return tasks.size();
    }
}
